package com.couldsail.takeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.couldsail.takeaway.mapper.ShopImagesMapper;
import com.couldsail.takeaway.mapper.ShopMapper;
import com.couldsail.takeaway.pojo.dto.ShopDetailDTO;
import com.couldsail.takeaway.pojo.entity.Shop;
import com.couldsail.takeaway.pojo.entity.ShopImages;
import com.couldsail.takeaway.pojo.vo.ShopVO;
import com.couldsail.takeaway.service.ShopService;
import com.couldsail.takeaway.utils.AddressLocationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ShopServiceImpl implements ShopService {
    
    private final ShopMapper shopMapper;
    private final ShopImagesMapper shopImagesMapper;
    
    @Override
    public List<ShopVO> getShopList() {
        // 查询正常营业的店铺
        LambdaQueryWrapper<Shop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Shop::getStatus, 1)
                .orderByDesc(Shop::getRating);
        
        return shopMapper.selectList(wrapper).stream().map(shop -> {
            ShopVO shopVO = new ShopVO();
            BeanUtils.copyProperties(shop, shopVO);
            
            // 查询店铺图片
            LambdaQueryWrapper<ShopImages> imageWrapper = new LambdaQueryWrapper<>();
            imageWrapper.eq(ShopImages::getShopId, shop.getId());
            List<ShopImages> images = shopImagesMapper.selectList(imageWrapper);
            shopVO.setImages(images);
            
            return shopVO;
        }).collect(Collectors.toList());
    }
    
    @Override
    public ShopVO getShopById(ShopDetailDTO shopDetailDTO) throws Exception {
        Shop shop = shopMapper.selectById(shopDetailDTO.getId());
        ShopVO shopVO = new ShopVO();
        BeanUtils.copyProperties(shop, shopVO);
        shopVO.setSeatNums(shop.getSeatNums().split(",\\s*"));
        shopVO.setAddress(shop.getDistrict()+shop.getAddress());
        shopVO.setSubCurrent(mapValue(shop.getSubCurrent()));
        // 查询店铺图片
        LambdaQueryWrapper<ShopImages> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShopImages::getShopId, shopDetailDTO.getId());
        List<ShopImages> images = shopImagesMapper.selectList(wrapper);
        shopVO.setImages(images);
        if (shopDetailDTO.getLongitude()!=null) {
            shopVO.setDistance(AddressLocationUtil.getDistance(shopDetailDTO.getLongitude(),shopDetailDTO.getLatitude(),shop.getLongitude(),shop.getLatitude()));
        }
        return shopVO;
    }

        private static final Map<Integer, String> intToStringMap = new HashMap<>();
        private static final Map<String, Integer> stringToIntMap = new HashMap<>();

        static {
            // 初始化映射关系
            intToStringMap.put(0, "dine_in");
            intToStringMap.put(1, "delivery");
            intToStringMap.put(2, "both");

            stringToIntMap.put("dine_in", 0);
            stringToIntMap.put("delivery", 1);
            stringToIntMap.put("both", 2);
        }

        public static Object mapValue(Object input) {
            if (input instanceof Integer) {
                Integer intValue = (Integer) input;
                if (intValue >= 0 && intValue <= 2) { // 检查是否在有效范围内
                    return intToStringMap.get(intValue);
                } else {
                    throw new IllegalArgumentException("Invalid integer value: " + intValue);
                }
            } else if (input instanceof String) {
                return stringToIntMap.getOrDefault(input, null); // 如果不存在，则返回null
            } else {
                throw new IllegalArgumentException("Unsupported type: " + input.getClass().getSimpleName());
            }
        }

    }